package com.googlecode.gingerman.world.geometry;

public class Vector {
	private Coordinates endPoint;

	public Vector(Coordinates endPoint) {
		this.endPoint = endPoint;
	}

	public Vector(double angle, double length) {
		Coordinates tmp;
		if (Math.sin(angle)==0) tmp = new Coordinates(length, 0); 
		else if (Math.cos(angle)==0) tmp = new Coordinates(0, length);
		else tmp = new Coordinates(length * Math.cos(angle), length * Math.sin(angle));		
		endPoint = tmp;
	}

	public double getX() {
		return endPoint.getX();
	}

	public double getY() {
		return endPoint.getY();
	}

	public double getLength() {
		return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY());
	}

	public double getAngle() {
		if (this.getX()==0) return (Math.PI/2);
		return Math.atan2(this.getY(), this.getX());
	}

	public Vector add(Vector vector) {
		return new Vector(new Coordinates(this.getX() + vector.getX(), this.getY() + vector.getY()));
	}

	public Vector substract(Vector vector) {
		return new Vector(new Coordinates(this.getX() - vector.getX(), this.getY() - vector.getY()));
	}

	public Vector multiplyByScalar(double scalar) {
		return new Vector(new Coordinates(this.getX() * scalar, this.getY() * scalar));
	}

	public Vector normalize() {
		return new Vector(new Coordinates(this.getX() / this.getLength(), this.getY() / this.getLength()));
	}
}
